package com.hf.www.base.model;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.hf.www.base.common.Pager;
import com.hf.www.core.kit.SqlXmlKit;
import com.hf.www.core.plugin.annotation.Table;
import com.hf.www.tools.SysConstants;
import com.jfinal.aop.Before;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * CREATE TABLE `sys_user` (
 * `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
 * `nick` varchar(30) DEFAULT NULL COMMENT '昵称',
 * `username` varchar(30) NOT NULL COMMENT '用户名',
 * `password` varchar(32) NOT NULL COMMENT '密码',
 * `email` varchar(100) DEFAULT NULL COMMENT 'Email',
 * `qq` varchar(20) DEFAULT NULL COMMENT 'QQ',
 * `phone` varchar(20) DEFAULT NULL COMMENT '电话',
 * `status` int(11) NOT NULL DEFAULT '0' COMMENT '状态 0-停用 1-正常',
 * `updateDate` datetime DEFAULT NULL COMMENT '修改时间',
 * `createDate` datetime NOT NULL COMMENT '创建时间',
 * PRIMARY KEY (`id`)
 * ) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
 */
@SuppressWarnings("serial")
@Table(tableName = "sys_user")
public class SysUserModel extends Model<SysUserModel> {

	public static final SysUserModel dao = new SysUserModel();

	public SysUserModel login(String username, String password) {
		return dao.findFirst("select * from sys_user where status=1 and username=? and password=?", username, HashKit.md5(password));
	}
	public List<SysUserModel> getAllUserName() {
		return dao.find("select id,nick from sys_user");
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Page<SysUserModel> page(Pager pager) {
		LinkedList<Object> param = new LinkedList<Object>();
		Page<SysUserModel> page = dao.paginate(pager.getPageNo(), pager.getPageSize(), 
				" select *,(select GROUP_CONCAT(DISTINCT(roleID)) from sys_user_role ur where u.id = ur.userID) roles " ,
				SqlXmlKit.getSql("SysUser.pager", (Map)pager.getParamsMap(),param),param.toArray());
		return page;
	}
	
	public void batchDel(String ids){
		
		if(ids == null || "".equals(ids.trim())){
			return ;
		}
		
		String arr[] = ids.split("\\,");
		for (String id : arr) {
			if("".equals(id)){
				continue ;
			}
			dao.deleteById(id);
		}
		
	}
	
	/**
	 * 根据用户ID获取带有系统角色的用户信息
	 * @param id
	 * @return
	 */
	public Record getUserById(Integer id){
		return Db.findFirst(" select *,(select GROUP_CONCAT(DISTINCT(roleID)) from sys_user_role ur where u.id = ur.userID) roles from sys_user u where 1=1 and u.id=? " , id);
	}
	
	
	/**
	 * 
	 * 用户信息的编辑保存
	 * 
	 * @param user
	 */
	
	@Before(Tx.class)
	public void editSave(SysUserModel user){

		if(user.getInt("id") != null){
			user.set("updateDate", new Date());
			user.update();
			
			
		}else{
			user.set("password", HashKit.md5(SysConstants.DEFAULT_PASSWORD));
			user.set("createDate", new Date());
			user.save(); 
		}
	
		SysUserRoleModel.dao.deleteByUser(user.get("id"));
		String roles = user.get("roles");
		if(StrKit.notBlank(roles)){
			String arr[] = roles.split("\\,");
			for (int i = 0; i < arr.length; i++) {
				SysUserRoleModel.dao.changeUserRole(new SysUserRoleModel().set("userID", user.get("id")).set("roleID", arr[i]), true);
			}
		}
	}
	
	
	
}
